﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Universe.OneC.DomainModel;

namespace Universe.OneC
{
    public class OneCMetadataReader
    {
        
        // Табличная часть
        private MetaClass TablePart;

        // Регистр накоплений
        private MetaClass AccRegBase;

        // Регистр сведений
        private MetaClass InfoRegBase;

        // Справочник
        private MetaClass CatalogBase;


        // хрень
        private MetaClass CharacteristicBase;
        
        // Документ
        private MetaClass DocumentBase;
        
        // Перечисление
        private MetaClass EnumBase;

        public MetaModel Get_1C_Metadata(Adapter1C adapter, InfoBase infobase)
        {
            MetaModel model = new MetaModel();
            adapter.Open(infobase);
            object metadata = adapter.GetMetadata();
            model.Name = (string)metadata.Get("Имя");
            model.Version = (string)metadata.Get("Версия");
            this.InitNamespaces(model);
            this.InitBaseClasses(model);
            this.LoadEnumerations(adapter, model, metadata);
            List<MetaClass> catalogs = this.LoadCatalogs(adapter, model, metadata);
            List<MetaClass> characteristics = this.LoadCharacteristics(adapter, model, metadata);
            this.LoadCatalogOwners(adapter, metadata, model, catalogs, characteristics);
            List<MetaClass> docs = this.LoadDocuments(adapter, model, metadata);
            List<MetaClass> inforegs = this.LoadInfoRegisters(adapter, model, metadata);
            List<MetaClass> accregs = this.LoadAccRegisters(adapter, model, metadata);
            this.LoadRegisterDocuments(adapter, metadata, model, docs, inforegs, accregs);
            // this.Get_SQL_Metadata(adapter, model, infobase, tracepath);
            adapter.ReleaseObject(ref metadata);
            // adapter.Dispose();
            // adapter = null;
            return model;
        }

        private List<MetaClass> LoadAccRegisters(Adapter1C adapter, MetaModel model, object metadata)
        {
            List<MetaClass> list = new List<MetaClass>();
            object target = metadata.Get("РегистрыНакопления");
            int num = (int)target.Call("Количество", null);
            for (int i = 0; i < num; i++)
            {
                object objAccReg = target.Call("Получить", new object[] { i });
                MetaClass class3 = new MetaClass(model, (string)objAccReg.Get("Имя"));
                class3.Kind = PersistentType.ValueObject;
                class3.BaseClass = this.AccRegBase;
                class3.Namespace = model.Namespaces[0].Children[5];
                class3.Discriminator = 1;
                MetaClass owner = class3;

                MetaProperty property3 = new MetaProperty(owner, "Document");
                property3.Purpose = DataPurpose.Reference | DataPurpose.CompoundKey;
                property3.Storage = "_Document";
                property3.Size = 0x10;
                property3.AllowsNULL = false;

                MetaProperty property = property3;
                MetaField field2 = new MetaField(property, "_RecorderRRef");
                field2.DbType = DbType.Binary;
                field2.DbName = "binary(16)";
                field2.CastToType = DbType.Guid;
                field2.CastToName = "uniqueidentifier";
                property.fReference = field2;
                owner.Properties.Add(property);


                object obj3 = objAccReg.Get("ВидРегистра");
                if (adapter.String(obj3) == "Остатки")
                {
                    MetaProperty property2 = new MetaProperty(owner, "DebitCredit");
                    property2.Purpose = DataPurpose.Primitive;
                    property2.Storage = "_DebitCredit";
                    property2.Size = 0x13;
                    property2.Precision = 1;
                    property2.Scale = 0;
                    property2.CanBeNegative = false;
                    property2.AllowsNULL = false;

                    List<System.Type> list2 = new List<System.Type>();
                    list2.Add(typeof(int));
                    property2.Types = list2;
                    List<string> list3 = new List<string>();
                    list3.Add("System.Int32");
                    property2.Names = list3;
                    property = property2;
                    MetaField field = new MetaField(property, "_RecordKind");
                    field.DbType = DbType.Decimal;
                    field.DbName = "numeric(1,0)";
                    field.CastToType = DbType.Int32;
                    field.CastToName = "int";
                    property.fNumeric = field;
                    owner.Properties.Add(property);
                }
                adapter.ReleaseObject(ref obj3);
                this.LoadCustomProperties(adapter, owner, objAccReg, "Измерения");
                this.LoadCustomProperties(adapter, owner, objAccReg, "Ресурсы");
                this.LoadCustomProperties(adapter, owner, objAccReg, "Реквизиты");
                list.Add(owner);
                model.Registry.Add(owner.FullName, owner);
                adapter.ReleaseObject(ref objAccReg);
            }
            adapter.ReleaseObject(ref target);
            return list;
        }


        private List<MetaClass> LoadInfoRegisters(Adapter1C adapter, MetaModel model, object metadata)
        {
            List<MetaClass> list = new List<MetaClass>();
            object target = metadata.Get("РегистрыСведений");
            int num = (int)target.Call("Количество", null);
            for (int i = 0; i < num; i++)
            {
                MetaProperty property;
                object obj2 = target.Call("Получить", new object[] { i });
                MetaClass class3 = new MetaClass(model, (string)obj2.Get("Имя"));
                class3.Kind = PersistentType.ValueObject;
                class3.Namespace = model.Namespaces[0].Children[4];
                class3.Discriminator = 1;
                MetaClass owner = class3;
                object obj3 = obj2.Get("ПериодичностьРегистраСведений");
                if (adapter.String(obj3) != "Непериодический")
                {
                    MetaProperty property2 = new MetaProperty(owner, "Period");
                    property2.Purpose = DataPurpose.Primitive | DataPurpose.CompoundKey;
                    property2.Storage = "_Period";
                    List<System.Type> list2 = new List<System.Type>();
                    list2.Add(typeof(DateTime));
                    property2.Types = list2;
                    List<string> list3 = new List<string>();
                    list3.Add("System.DateTime");
                    property2.Names = list3;
                    property2.Size = 8;
                    property2.DatePart = DatePart.DateTime;
                    property2.Periodicity = Periodicity.Second;
                    property2.AllowsNULL = false;
                    property = property2;
                    MetaField field = new MetaField(property, "_Period");
                    field.DbType = DbType.DateTime;
                    field.DbName = "datetime";
                    field.CastToType = DbType.DateTime;
                    field.CastToName = "datetime";
                    property.fDateTime = field;
                    owner.Properties.Add(property);
                }
                adapter.ReleaseObject(ref obj3);
                obj3 = obj2.Get("РежимЗаписи");
                if (adapter.String(obj3) == "ПодчинениеРегистратору")
                {
                    MetaProperty property3 = new MetaProperty(owner, "IsActive");
                    property3.Purpose = DataPurpose.Primitive;
                    property3.Storage = "_IsActive";
                    List<System.Type> list4 = new List<System.Type>();
                    list4.Add(typeof(bool));
                    property3.Types = list4;
                    List<string> list5 = new List<string>();
                    list5.Add("System.Boolean");
                    property3.Names = list5;
                    property3.Size = 1;
                    property3.AllowsNULL = false;
                    property = property3;
                    MetaField field2 = new MetaField(property, "_Active");
                    field2.DbType = DbType.Binary;
                    field2.DbName = "binary(1)";
                    field2.CastToType = DbType.Boolean;
                    field2.CastToName = "bit";
                    property.fBoolean = field2;
                    owner.Properties.Add(property);
                    MetaProperty property4 = new MetaProperty(owner, "Document");
                    property4.Purpose = DataPurpose.Reference | DataPurpose.CompoundKey;
                    property4.Storage = "_Document";
                    property4.Size = 0x10;
                    property4.AllowsNULL = false;
                    property = property4;
                    MetaField field3 = new MetaField(property, "_RecorderRRef");
                    field3.DbType = DbType.Binary;
                    field3.DbName = "binary(16)";
                    field3.CastToType = DbType.Guid;
                    field3.CastToName = "uniqueidentifier";
                    property.fReference = field3;
                    owner.Properties.Add(property);
                    MetaProperty property5 = new MetaProperty(owner, "LineNumber");
                    property5.Purpose = DataPurpose.Primitive | DataPurpose.CompoundKey;
                    property5.Storage = "_LineNumber";
                    property5.Size = 0x13;
                    property5.Precision = 9;
                    property5.Scale = 0;
                    property5.CanBeNegative = false;
                    property5.AllowsNULL = false;
                    List<System.Type> list6 = new List<System.Type>();
                    list6.Add(typeof(int));
                    property5.Types = list6;
                    List<string> list7 = new List<string>();
                    list7.Add("System.Int32");
                    property5.Names = list7;
                    property = property5;
                    MetaField field4 = new MetaField(property, "_LineNo");
                    field4.DbType = DbType.Decimal;
                    field4.DbName = "numeric(9,0)";
                    field4.CastToType = DbType.Int32;
                    field4.CastToName = "int";
                    property.fNumeric = field4;
                    owner.Properties.Add(property);
                }
                adapter.ReleaseObject(ref obj3);
                obj3 = obj2.Get("Измерения");
                if (((int)obj3.Call("Количество", null)) > 1)
                {
                    MetaProperty property6 = new MetaProperty(owner, "SimpleKey");
                    property6.Purpose = DataPurpose.Identity;
                    property6.Storage = "_SimpleKey";
                    List<System.Type> list8 = new List<System.Type>();
                    list8.Add(typeof(Guid));
                    property6.Types = list8;
                    List<string> list9 = new List<string>();
                    list9.Add("System.Guid");
                    property6.Names = list9;
                    property6.Size = 0x10;
                    property6.AllowsNULL = false;
                    property = property6;
                    MetaField field5 = new MetaField(property, "_SimpleKey");
                    field5.DbType = DbType.Binary;
                    field5.DbName = "binary(16)";
                    field5.CastToType = DbType.Guid;
                    field5.CastToName = "uniqueidentifier";
                    property.fIdentity = field5;
                    owner.Properties.Add(property);
                }
                adapter.ReleaseObject(ref obj3);
                this.LoadCustomProperties(adapter, owner, obj2, "Измерения");
                this.LoadCustomProperties(adapter, owner, obj2, "Ресурсы");
                this.LoadCustomProperties(adapter, owner, obj2, "Реквизиты");
                list.Add(owner);
                model.Registry.Add(owner.FullName, owner);
                adapter.ReleaseObject(ref obj2);
            }
            adapter.ReleaseObject(ref target);
            return list;
        }

        private void LoadRegisterDocuments(Adapter1C adapter, object metadata, MetaModel model, List<MetaClass> docs, List<MetaClass> inforegs, List<MetaClass> accregs)
        {
            string str;
            int num2;
            MetaClass class2;
            object[] objArray = new object[docs.Count];
            int count = inforegs.Count + accregs.Count;
            object[] objArray2 = new object[count];
            count = docs.Count;
            for (num2 = 0; num2 < count; num2++)
            {
                class2 = docs[num2];
                str = "Документ." + class2.Name;
                objArray[num2] = metadata.Call("НайтиПоПолномуИмени", new object[] { str });
            }
            count = inforegs.Count + accregs.Count;
            int num4 = inforegs.Count;
            for (num2 = 0; num2 < count; num2++)
            {
                if (num2 < num4)
                {
                    class2 = inforegs[num2];
                    str = "РегистрСведений." + class2.Name;
                }
                else
                {
                    class2 = accregs[num2 - num4];
                    str = "РегистрНакопления." + class2.Name;
                }
                objArray2[num2] = metadata.Call("НайтиПоПолномуИмени", new object[] { str });
            }
            count = docs.Count;
            for (num2 = 0; num2 < count; num2++)
            {
                object target = objArray[num2].Get("Движения");
                int num3 = (int)target.Call("Количество", null);
                if (num3 > 0)
                {
                    num4 = 0;
                    int num5 = inforegs.Count + accregs.Count;
                    for (int i = 0; i < num5; i++)
                    {
                        if ((bool)target.Call("Содержит", new object[] { objArray2[i] }))
                        {
                            if (i < inforegs.Count)
                            {
                                class2 = inforegs[i];
                            }
                            else
                            {
                                class2 = accregs[i - inforegs.Count];
                            }
                            MetaProperty property = class2.Properties.Where<MetaProperty>(delegate(MetaProperty p)
                            {
                                return (p.Name == "Document");
                            }).Single<MetaProperty>();
                            if (property.Names == null)
                            {
                                property.Names = new List<string>();
                            }
                            property.Names.Add(model.Namespaces[0].Name + "." + ("Документы." + docs[num2].Name));
                            if (property.Names.Count > 1)
                            {
                                property.Purpose = DataPurpose.Chameleon | DataPurpose.CompoundKey;
                                MetaField field = new MetaField(property, "_RecorderTRef");
                                field.DbType = DbType.Binary;
                                field.DbName = "binary(16)";
                                field.CastToType = DbType.Guid;
                                field.CastToName = "uniqueidentifier";
                                property.fDiscriminator = field;
                            }
                            num4++;
                            if (num4 == num3)
                            {
                                break;
                            }
                        }
                    }
                }
                adapter.ReleaseObject(ref target);
            }
            count = docs.Count;
            for (num2 = 0; num2 < count; num2++)
            {
                adapter.ReleaseObject(ref objArray[num2]);
            }
            objArray = null;
            count = inforegs.Count + accregs.Count;
            for (num2 = 0; num2 < count; num2++)
            {
                adapter.ReleaseObject(ref objArray2[num2]);
            }
            objArray2 = null;
        }

        private List<MetaClass> LoadCatalogs(Adapter1C adapter, MetaModel model, object metadata)
        {
            List<MetaClass> list = new List<MetaClass>();
            object target = metadata.Get("Справочники");
            int num2 = (int)target.Call("Количество", null);
            for (int i = 0; i < num2; i++)
            {
                MetaProperty property;
                object rawCatalogMetadata = target.Call("Получить", new object[] { i });
                MetaClass metaCatalog = new MetaClass(model, (string)rawCatalogMetadata.Get("Имя"));
                metaCatalog.Synonym = (string)rawCatalogMetadata.Get("Synonym");

                metaCatalog.Kind = PersistentType.ReferenceObject;
                metaCatalog.BaseClass = this.CatalogBase;
                metaCatalog.Namespace = model.Namespaces[0].Children[1];
                metaCatalog.Discriminator = 1;
                MetaClass owner = metaCatalog;
                int num = (int)rawCatalogMetadata.Get("ДлинаКода");
                if (num > 0)
                {
                    MetaProperty property2 = new MetaProperty(owner, "Code");
                    property2.Purpose = DataPurpose.Primitive;
                    property2.Storage = "_Code";
                    property2.Size = num;
                    property2.AllowsNULL = false;
                    property = property2;
                    if (adapter.String(rawCatalogMetadata.Get("ТипКода")) == "Строка")
                    {
                        property.HasFixedLength = true;
                        List<System.Type> list2 = new List<System.Type>();
                        list2.Add(typeof(string));
                        property.Types = list2;
                        List<string> list3 = new List<string>();
                        list3.Add("System.String");
                        property.Names = list3;
                        MetaField field = new MetaField(property, "_Code");
                        field.DbType = DbType.StringFixedLength;
                        field.DbName = "nchar(" + num.ToString() + ")";
                        field.CastToType = DbType.StringFixedLength;
                        field.CastToName = "nchar(" + num.ToString() + ")";
                        property.fString = field;
                    }
                    else
                    {
                        property.Precision = num;
                        property.Scale = 0;
                        property.CanBeNegative = false;
                        List<System.Type> list4 = new List<System.Type>();
                        list4.Add(typeof(int));
                        property.Types = list4;
                        List<string> list5 = new List<string>();
                        list5.Add("System.Int32");
                        property.Names = list5;
                        MetaField field2 = new MetaField(property, "_Code");
                        field2.DbType = DbType.Decimal;
                        field2.DbName = "numeric(" + num.ToString() + ",0)";
                        field2.CastToType = DbType.Int32;
                        field2.CastToName = "int";
                        property.fNumeric = field2;
                    }
                    owner.Properties.Add(property);
                }
                num = (int)rawCatalogMetadata.Get("ДлинаНаименования");
                if (num > 0)
                {
                    MetaProperty property3 = new MetaProperty(owner, "Description");
                    property3.Purpose = DataPurpose.Primitive;
                    property3.Storage = "_Name";
                    List<System.Type> list6 = new List<System.Type>();
                    list6.Add(typeof(string));
                    property3.Types = list6;
                    List<string> list7 = new List<string>();
                    list7.Add("System.String");
                    property3.Names = list7;
                    property3.Size = num;
                    property3.AllowsNULL = false;
                    property3.HasFixedLength = false;
                    property = property3;
                    MetaField field3 = new MetaField(property, "_Description");
                    field3.DbType = DbType.String;
                    field3.DbName = "nvarchar(" + num.ToString() + ")";
                    field3.CastToType = DbType.String;
                    field3.CastToName = "nvarchar(" + num.ToString() + ")";
                    property.fString = field3;
                    owner.Properties.Add(property);
                }
                if ((bool)rawCatalogMetadata.Get("Иерархический"))
                {
                    MetaProperty property5 = new MetaProperty(owner, "Parent");
                    property5.Purpose = DataPurpose.Reference;
                    property5.Storage = "_Parent";
                    property5.Size = 0x10;
                    property5.AllowsNULL = false;
                    List<string> list10 = new List<string>();
                    list10.Add(owner.FullName);
                    property5.Names = list10;
                    property = property5;
                    MetaField field5 = new MetaField(property, "_ParentIDRRef");
                    field5.DbType = DbType.Binary;
                    field5.DbName = "binary(16)";
                    field5.CastToType = DbType.Guid;
                    field5.CastToName = "uniqueidentifier";
                    property.fReference = field5;
                    owner.Properties.Add(property);
                    if (adapter.String(rawCatalogMetadata.Get("ВидИерархии")) == "ИерархияГруппИЭлементов")
                    {
                        MetaProperty property4 = new MetaProperty(owner, "IsFolder");
                        property4.Purpose = DataPurpose.Primitive;
                        property4.Storage = "_IsFolder";
                        property4.Size = 1;
                        List<System.Type> list8 = new List<System.Type>();
                        list8.Add(typeof(bool));
                        property4.Types = list8;
                        List<string> list9 = new List<string>();
                        list9.Add("System.Boolean");
                        property4.Names = list9;
                        property4.AllowsNULL = false;
                        property = property4;
                        MetaField field4 = new MetaField(property, "_Folder");
                        field4.DbType = DbType.Binary;
                        field4.DbName = "binary(1)";
                        field4.CastToType = DbType.Boolean;
                        field4.CastToName = "bit";
                        property.fBoolean = field4;
                        owner.Properties.Add(property);
                    }
                }
                object obj4 = rawCatalogMetadata.Get("Владельцы");
                num = (int)obj4.Call("Количество", null);
                if (num == 1)
                {
                    MetaProperty property6 = new MetaProperty(owner, "Owner");
                    property6.Purpose = DataPurpose.Reference;
                    property6.Storage = "_Owner";
                    property6.Size = 0x10;
                    property6.AllowsNULL = false;
                    property = property6;
                    MetaField field6 = new MetaField(property, "_OwnerIDRRef");
                    field6.DbType = DbType.Binary;
                    field6.DbName = "binary(16)";
                    field6.CastToType = DbType.Guid;
                    field6.CastToName = "uniqueidentifier";
                    property.fReference = field6;
                    owner.Properties.Add(property);
                }
                else if (num > 1)
                {
                    MetaProperty property7 = new MetaProperty(owner, "Owner");
                    property7.Purpose = DataPurpose.Chameleon;
                    property7.Storage = "_Owner";
                    property7.Size = 0x10;
                    property7.AllowsNULL = false;
                    property = property7;
                    MetaField field7 = new MetaField(property, "_OwnerID_RTRef");
                    field7.DbType = DbType.Binary;
                    field7.DbName = "binary(4)";
                    field7.CastToType = DbType.Int32;
                    field7.CastToName = "int";
                    property.fDiscriminator = field7;
                    MetaField field8 = new MetaField(property, "_OwnerID_RRRef");
                    field8.DbType = DbType.Binary;
                    field8.DbName = "binary(16)";
                    field8.CastToType = DbType.Guid;
                    field8.CastToName = "uniqueidentifier";
                    property.fReference = field8;
                    MetaField field9 = new MetaField(property, "_OwnerID_TYPE");
                    field9.DbType = DbType.Binary;
                    field9.DbName = "binary(1)";
                    field9.CastToType = DbType.Byte;
                    field9.CastToName = "tinyint";
                    property.fBinary = field9;
                    owner.Properties.Add(property);
                }
                adapter.ReleaseObject(ref obj4);
                this.LoadCustomProperties(adapter, owner, rawCatalogMetadata, "Реквизиты");
                this.LoadTableParts(adapter, model, owner, rawCatalogMetadata);
                list.Add(owner);
                model.Registry.Add(owner.FullName, owner);
                adapter.ReleaseObject(ref rawCatalogMetadata);
            }
            adapter.ReleaseObject(ref target);
            return list;
        }

        private void LoadTableParts(Adapter1C adapter, MetaModel model, MetaClass owner, object source)
        {
            object target = source.Get("ТабличныеЧасти");
            int num = (int)target.Call("Количество", null);
            for (int i = 0; i < num; i++)
            {
                object obj2 = target.Call("Получить", new object[] { i });
                MetaClass metaTablePart = new MetaClass(model, (string)obj2.Get("Имя"));
                metaTablePart.Synonym = (string) obj2.Get("Synonym");

                metaTablePart.Kind = PersistentType.DependentObject;
                metaTablePart.Namespace = owner.Namespace;
                metaTablePart.BaseClass = this.TablePart;
                metaTablePart.Owner = owner;
                metaTablePart.Discriminator = 1;

                MetaProperty property = null;

/*
                MetaProperty property2 = new MetaProperty(class2, "Ссылка");
                property2.Purpose = DataPurpose.Reference | DataPurpose.CompoundKey | DataPurpose.Aggregate;
                property2.Storage = "_Owner";
                property2.Size = 0x10;
                List<string> list = new List<string>();
                list.Add(owner.FullName);
                property2.Names = list;
                property2.AllowsNULL = false;
                MetaProperty property = property2;
                MetaField field = new MetaField(property, "_Fld?");
                field.DbType = DbType.Binary;
                field.DbName = "binary(16)";
                field.CastToType = DbType.Guid;
                field.CastToName = "uniqueidentifier";
                property.fReference = field;
                class2.Properties.Add(property);
*/


                MetaProperty property3 = new MetaProperty(metaTablePart, "НомерСтроки");
                property3.Purpose = DataPurpose.Primitive | DataPurpose.CompoundKey;
                property3.Storage = "_RowNumber";
                property3.Size = 4;
                property3.Precision = 5;
                property3.Scale = 0;
                property3.CanBeNegative = false;
                List<System.Type> list2 = new List<System.Type>();
                list2.Add(typeof(int));
                property3.Types = list2;
                List<string> list3 = new List<string>();
                list3.Add("System.Int32");
                property3.Names = list3;
                property = property3;
                MetaField field2 = new MetaField(property, "_Fld?");
                field2.DbType = DbType.Decimal;
                field2.DbName = "numeric(5,0)";
                field2.CastToType = DbType.Int32;
                field2.CastToName = "int";
                property.fNumeric = field2;
                metaTablePart.Properties.Add(property);
                this.LoadCustomProperties(adapter, metaTablePart, obj2, "Реквизиты");
                model.Registry.Add(metaTablePart.FullName, metaTablePart);
                adapter.ReleaseObject(ref obj2);
            }
            adapter.ReleaseObject(ref target);
        }


        private List<MetaClass> LoadCharacteristics(Adapter1C adapter, MetaModel model, object metadata)
        {
            List<MetaClass> list = new List<MetaClass>();
            object target = metadata.Get("ПланыВидовХарактеристик");
            int num = (int)target.Call("Количество", null);
            for (int i = 0; i < num; i++)
            {
                MetaProperty property;
                object obj2 = target.Call("Получить", new object[] { i });
                MetaClass class3 = new MetaClass(model, (string)obj2.Get("Имя"));
                class3.Kind = PersistentType.ReferenceObject;
                class3.BaseClass = this.CharacteristicBase;
                class3.Namespace = model.Namespaces[0].Children[3];
                class3.Discriminator = 1;
                MetaClass owner = class3;
                int num3 = (int)obj2.Get("ДлинаКода");
                if (num3 > 0)
                {
                    MetaProperty property2 = new MetaProperty(owner, "Code");
                    property2.Purpose = DataPurpose.Primitive;
                    property2.Storage = "_Code";
                    property2.Size = num3;
                    property2.AllowsNULL = false;
                    property2.HasFixedLength = true;
                    List<System.Type> list2 = new List<System.Type>();
                    list2.Add(typeof(string));
                    property2.Types = list2;
                    List<string> list3 = new List<string>();
                    list3.Add("System.String");
                    property2.Names = list3;
                    property = property2;
                    MetaField field = new MetaField(property, "_Code");
                    field.DbType = DbType.StringFixedLength;
                    field.DbName = "nchar(" + num3.ToString() + ")";
                    field.CastToType = DbType.StringFixedLength;
                    field.CastToName = "nchar(" + num3.ToString() + ")";
                    property.fString = field;
                    owner.Properties.Add(property);
                }
                num3 = (int)obj2.Get("ДлинаНаименования");
                if (num3 > 0)
                {
                    MetaProperty property3 = new MetaProperty(owner, "Name");
                    property3.Purpose = DataPurpose.Primitive;
                    property3.Storage = "_Name";
                    List<System.Type> list4 = new List<System.Type>();
                    list4.Add(typeof(string));
                    property3.Types = list4;
                    List<string> list5 = new List<string>();
                    list5.Add("System.String");
                    property3.Names = list5;
                    property3.Size = num3;
                    property3.AllowsNULL = false;
                    property3.HasFixedLength = false;
                    property = property3;
                    MetaField field2 = new MetaField(property, "_Description");
                    field2.DbType = DbType.String;
                    field2.DbName = "nvarchar(" + num3.ToString() + ")";
                    field2.CastToType = DbType.String;
                    field2.CastToName = "nvarchar(" + num3.ToString() + ")";
                    property.fString = field2;
                    owner.Properties.Add(property);
                }
                if ((bool)obj2.Get("Иерархический"))
                {
                    MetaProperty property4 = new MetaProperty(owner, "Parent");
                    property4.Purpose = DataPurpose.Reference;
                    property4.Storage = "_Parent";
                    property4.Size = 0x10;
                    property4.AllowsNULL = false;
                    List<string> list6 = new List<string>();
                    list6.Add(owner.FullName);
                    property4.Names = list6;
                    property = property4;
                    MetaField field3 = new MetaField(property, "_ParentIDRRef");
                    field3.DbType = DbType.Binary;
                    field3.DbName = "binary(16)";
                    field3.CastToType = DbType.Guid;
                    field3.CastToName = "uniqueidentifier";
                    property.fReference = field3;
                    owner.Properties.Add(property);
                    MetaProperty property5 = new MetaProperty(owner, "IsFolder");
                    property5.Purpose = DataPurpose.Primitive;
                    property5.Storage = "_IsFolder";
                    property5.Size = 1;
                    List<System.Type> list7 = new List<System.Type>();
                    list7.Add(typeof(bool));
                    property5.Types = list7;
                    List<string> list8 = new List<string>();
                    list8.Add("System.Boolean");
                    property5.Names = list8;
                    property5.AllowsNULL = false;
                    property = property5;
                    MetaField field4 = new MetaField(property, "_Folder");
                    field4.DbType = DbType.Binary;
                    field4.DbName = "binary(1)";
                    field4.CastToType = DbType.Boolean;
                    field4.CastToName = "bit";
                    property.fBoolean = field4;
                    owner.Properties.Add(property);
                }
                this.LoadCustomProperties(adapter, owner, obj2, "Реквизиты");
                this.LoadTableParts(adapter, model, owner, obj2);
                list.Add(owner);
                model.Registry.Add(owner.FullName, owner);
                adapter.ReleaseObject(ref obj2);
            }
            adapter.ReleaseObject(ref target);
            return list;
        }

        private void LoadCustomProperties(Adapter1C adapter, MetaClass owner, object metadata, string kind)
        {
            object target = null;
            object obj3 = metadata.Get(kind);
            int num = (int)obj3.Call("Количество", null);
            for (int i = 0; i < num; i++)
            {
                target = obj3.Call("Получить", new object[] { i });
                string name = (string)target.Get("Имя");
                MetaProperty property2 = new MetaProperty(owner, name);
                property2.Storage = "_" + name;
                property2.AllowsNULL = true;
                MetaProperty property = property2;
                this.ResolvePropertyType(adapter, property, target);
                if (kind == "Измерения")
                {
                    property.Purpose |= DataPurpose.CompoundKey;
                }
                owner.Properties.Add(property);
                adapter.ReleaseObject(ref target);
            }
            adapter.ReleaseObject(ref obj3);
        }


        private void ResolvePropertyType(Adapter1C adapter, MetaProperty property, object source)
        {
            string name = property.Class.Model.Namespaces[0].Name;
            property.Synonym = (string) source.Get("Synonym");
            object target = source.Get("Тип");
            object obj3 = target.Get("КвалификаторыСтроки");
            object obj4 = target.Get("КвалификаторыЧисла");
            object obj5 = target.Get("КвалификаторыДаты");
            object objTypes = target.Call("Типы", null);
            int capacity = (int)objTypes.Call("Количество", null);
            property.Purpose = (capacity > 1) ? DataPurpose.Chameleon : DataPurpose.None;
            property.Types = new List<System.Type>(capacity);
            property.Names = new List<string>(capacity);
            if (property.Purpose == DataPurpose.Chameleon)
            {
                MetaField field = new MetaField(property, "_Fld?");
                field.DbType = DbType.Binary;
                field.DbName = "binary(1)";
                field.CastToType = DbType.Byte;
                field.CastToName = "tinyint";
                property.fBinary = field;
                MetaField field2 = new MetaField(property, "_Fld?");
                field2.DbType = DbType.Binary;
                field2.DbName = "binary(4)";
                field2.CastToType = DbType.Int32;
                field2.CastToName = "int";
                property.fDiscriminator = field2;
            }
            for (int i = 0; i < capacity; i++)
            {
                string str3;
                MetaField field6;
                object type = objTypes.Call("Получить", new object[] { i });
                string str2 = adapter.XMLType(type);
                string str4 = str2;
                if (str4 == null)
                {
                    goto Label_0682;
                }
                if (!(str4 == "string"))
                {
                    if (str4 == "decimal")
                    {
                        goto Label_02BE;
                    }
                    if (str4 == "dateTime")
                    {
                        goto Label_0483;
                    }
                    if (str4 == "boolean")
                    {
                        goto Label_056B;
                    }
                    if (str4 == "ValueStorage")
                    {
                        goto Label_05F0;
                    }
                    goto Label_0682;
                }
                property.Purpose = (property.Purpose == DataPurpose.None) ? DataPurpose.Primitive : DataPurpose.Chameleon;
                property.Types.Add(typeof(string));
                property.Names.Add("System.String");
                property.Size = (int)obj3.Get("Длина");
                property.HasFixedLength = adapter.String(obj3.Get("ДопустимаяДлина")) == "Фиксированная";
                if (property.Size == 0)
                {
                    str3 = "ntext";
                }
                else if (property.HasFixedLength)
                {
                    str3 = "nchar(" + property.Size.ToString() + ")";
                }
                else
                {
                    str3 = "nvarchar(" + property.Size.ToString() + ")";
                }
                MetaField field4 = new MetaField(property, "_Fld?");
                field4.DbType = DbType.String;
                field4.DbName = str3;
                field4.CastToType = DbType.String;
                field4.CastToName = str3;
                property.fString = field4;
                goto Label_0810;
            Label_02BE:
                property.Purpose = (property.Purpose == DataPurpose.None) ? DataPurpose.Primitive : DataPurpose.Chameleon;
                property.Precision = (int)obj4.Get("Разрядность");
                property.Scale = (int)obj4.Get("РазрядностьДробнойЧасти");
                property.CanBeNegative = adapter.String(obj4.Get("ДопустимыйЗнак")) == "Любой";
                property.HasFixedLength = true;
                if (((property.Scale == 0) && !property.CanBeNegative) && (property.Precision < 10))
                {
                    str3 = "int";
                    property.Size = 4;
                    property.Types.Add(typeof(int));
                    property.Names.Add("System.Int32");
                }
                else
                {
                    str3 = "numeric(" + property.Precision.ToString() + "," + property.Scale.ToString() + ")";
                    property.Size = 0x13;
                    property.Types.Add(typeof(decimal));
                    property.Names.Add("System.Decimal");
                }
                MetaField field5 = new MetaField(property, "_Fld?");
                field5.DbType = DbType.Decimal;
                field5.DbName = "numeric(" + property.Precision.ToString() + "," + property.Scale.ToString() + ")";
                field5.CastToType = (str3 == "int") ? DbType.Int32 : DbType.Decimal;
                field5.CastToName = str3;
                property.fNumeric = field5;
                goto Label_0810;
            Label_0483:
                property.Purpose = (property.Purpose == DataPurpose.None) ? DataPurpose.Primitive : DataPurpose.Chameleon;
                property.Types.Add(typeof(DateTime));
                property.Names.Add("System.DateTime");
                property.Size = 8;
                property.Periodicity = Periodicity.Second;
                string str5 = adapter.String(obj5.Get("ЧастиДаты"));
                if (str5 != null)
                {
                    if (!(str5 == "Время"))
                    {
                        if (str5 == "Дата")
                        {
                            goto Label_0519;
                        }
                        if (str5 == "ДатаВремя")
                        {
                            goto Label_0522;
                        }
                    }
                    else
                    {
                        property.DatePart = DatePart.Time;
                    }
                }
                goto Label_0529;
            Label_0519:
                property.DatePart = DatePart.Date;
                goto Label_0529;
            Label_0522:
                property.DatePart = DatePart.DateTime;
            Label_0529:
                field6 = new MetaField(property, "_Fld?");
                field6.DbType = DbType.DateTime;
                field6.DbName = "datetime";
                field6.CastToType = DbType.DateTime;
                field6.CastToName = "datetime";
                property.fDateTime = field6;
                goto Label_0810;
            Label_056B:
                property.Purpose = (property.Purpose == DataPurpose.None) ? DataPurpose.Primitive : DataPurpose.Chameleon;
                property.Types.Add(typeof(bool));
                property.Names.Add("System.Boolean");
                property.Size = 1;
                MetaField field7 = new MetaField(property, "_Fld?");
                field7.DbType = DbType.Binary;
                field7.DbName = "binary(1)";
                field7.CastToType = DbType.Boolean;
                field7.CastToName = "bit";
                property.fBoolean = field7;
                goto Label_0810;
            Label_05F0:
                property.Purpose = (property.Purpose == DataPurpose.None) ? DataPurpose.Primitive : DataPurpose.Chameleon;
                if (property.Purpose == DataPurpose.Primitive)
                {
                    property.Types.Add(typeof(byte[]));
                    property.Names.Add("System.Byte[]");
                    property.Size = 0;
                    MetaField field3 = new MetaField(property, "_Fld?");
                    field3.DbType = DbType.Binary;
                    field3.DbName = "image";
                    field3.CastToType = DbType.Binary;
                    field3.CastToName = "image";
                    property.fBinary = field3;
                }
                goto Label_0810;
            Label_0682:
                property.Purpose = (property.Purpose == DataPurpose.None) ? DataPurpose.Reference : DataPurpose.Chameleon;
                property.Size = 0x10;
                MetaField field8 = new MetaField(property, "_Fld?");
                field8.DbType = DbType.Binary;
                field8.DbName = "binary(16)";
                field8.CastToType = DbType.Guid;
                field8.CastToName = "uniqueidentifier";
                property.fReference = field8;
                if (str2.StartsWith("EnumRef"))
                {
                    property.Types.Add(null);
                    property.Names.Add(name + "." + str2.Replace("EnumRef", "Перечисления"));
                }
                else if (str2.StartsWith("CatalogRef"))
                {
                    property.Types.Add(null);
                    property.Names.Add(name + "." + str2.Replace("CatalogRef", "Справочники"));
                }
                else if (str2.StartsWith("DocumentRef"))
                {
                    property.Types.Add(null);
                    property.Names.Add(name + "." + str2.Replace("DocumentRef", "Документы"));
                }
                else if (str2.StartsWith("ChartOfCharacteristicTypesRef"))
                {
                    property.Types.Add(null);
                    property.Names.Add(name + "." + str2.Replace("ChartOfCharacteristicTypesRef", "Характеристики"));
                }
                else
                {
                    property.Types.Add(null);
                    property.Names.Add("Unknown type");
                }
            Label_0810:
                adapter.ReleaseObject(ref type);
            }
            adapter.ReleaseObject(ref objTypes);
            adapter.ReleaseObject(ref target);
            adapter.ReleaseObject(ref obj3);
            adapter.ReleaseObject(ref obj4);
            adapter.ReleaseObject(ref obj5);
        }
        
        
        private List<MetaClass> LoadDocuments(Adapter1C adapter, MetaModel model, object metadata)
        {
            List<MetaClass> list = new List<MetaClass>();
            object target = metadata.Get("Документы");
            int num = (int)target.Call("Количество", null);
            for (int i = 0; i < num; i++)
            {
                object obj2 = target.Call("Получить", new object[] { i });
                MetaClass class3 = new MetaClass(model, (string)obj2.Get("Имя"));
                class3.Synonym = (string) obj2.Get("Synonym");

                class3.Kind = PersistentType.ReferenceObject;
                class3.BaseClass = this.DocumentBase;
                class3.Namespace = model.Namespaces[0].Children[2];
                class3.Discriminator = 1;
                MetaClass owner = class3;
                int num3 = (int)obj2.Get("ДлинаНомера");
                if (num3 > 0)
                {
                    MetaProperty property2 = new MetaProperty(owner, "Number");
                    property2.Purpose = DataPurpose.Primitive;
                    property2.Storage = "_Number";
                    property2.Size = num3;
                    property2.AllowsNULL = false;
                    MetaProperty property = property2;
                    object obj4 = obj2.Get("ТипНомера");
                    if (adapter.String(obj4) == "Строка")
                    {
                        property.HasFixedLength = true;
                        List<System.Type> list2 = new List<System.Type>();
                        list2.Add(typeof(string));
                        property.Types = list2;
                        List<string> list3 = new List<string>();
                        list3.Add("System.String");
                        property.Names = list3;
                        MetaField field = new MetaField(property, "_Number");
                        field.DbType = DbType.StringFixedLength;
                        field.DbName = "nchar(" + num3.ToString() + ")";
                        field.CastToType = DbType.StringFixedLength;
                        field.CastToName = "nchar(" + num3.ToString() + ")";
                        property.fString = field;
                    }
                    else
                    {
                        property.Size = 0x13;
                        property.Precision = num3;
                        property.Scale = 0;
                        property.CanBeNegative = false;
                        List<System.Type> list4 = new List<System.Type>();
                        list4.Add(typeof(int));
                        property.Types = list4;
                        List<string> list5 = new List<string>();
                        list5.Add("System.Int32");
                        property.Names = list5;
                        MetaField field2 = new MetaField(property, "_Number");
                        field2.DbType = DbType.Decimal;
                        field2.DbName = "numeric(" + num3.ToString() + ",0)";
                        field2.CastToType = DbType.Int32;
                        field2.CastToName = "int";
                        property.fNumeric = field2;
                    }
                    adapter.ReleaseObject(ref obj4);
                    object obj5 = obj2.Get("ПериодичностьНомера");
                    if (adapter.String(obj5) != "Непериодический")
                    {
                        MetaField field3 = new MetaField(property, "_NumberPrefix");
                        field3.DbType = DbType.DateTime;
                        field3.DbName = "datetime";
                        field3.CastToType = DbType.DateTime;
                        field3.CastToName = "datetime";
                        property.fDateTime = field3;
                    }
                    adapter.ReleaseObject(ref obj5);
                    owner.Properties.Add(property);
                }
                this.LoadCustomProperties(adapter, owner, obj2, "Реквизиты");
                this.LoadTableParts(adapter, model, owner, obj2);
                list.Add(owner);
                model.Registry.Add(owner.FullName, owner);
                adapter.ReleaseObject(ref obj2);
            }
            adapter.ReleaseObject(ref target);
            return list;
        }

        private void LoadCatalogOwners(Adapter1C adapter, object metadata, MetaModel model, List<MetaClass> catalogs, List<MetaClass> characteristics)
        {
            MetaClass class2;
            string str;
            int num2;
            int num = catalogs.Count + characteristics.Count;
            object[] objArray = new object[num];
            for (num2 = 0; num2 < num; num2++)
            {
                if (num2 < catalogs.Count)
                {
                    class2 = catalogs[num2];
                    str = "Справочник." + class2.Name;
                }
                else
                {
                    class2 = characteristics[num2 - catalogs.Count];
                    str = "ПланВидовХарактеристик." + class2.Name;
                }
                objArray[num2] = metadata.Call("НайтиПоПолномуИмени", new object[] { str });
            }
            for (num2 = 0; num2 < catalogs.Count; num2++)
            {
                object target = objArray[num2].Get("Владельцы");
                int capacity = (int)target.Call("Количество", null);
                if (capacity > 0)
                {
                    int num4 = 0;
                    for (int i = 0; i < num; i++)
                    {
                        if ((num2 != i) && ((bool)target.Call("Содержит", new object[] { objArray[i] })))
                        {
                            if (i < catalogs.Count)
                            {
                                class2 = catalogs[i];
                                str = "Справочники." + class2.Name;
                            }
                            else
                            {
                                class2 = characteristics[i - catalogs.Count];
                                str = "Характеристики." + class2.Name;
                            }
                            MetaProperty property = catalogs[num2].Properties.Where<MetaProperty>(delegate(MetaProperty p)
                            {
                                return (p.Name == "Owner");
                            }).Single<MetaProperty>();
                            if (property.Names == null)
                            {
                                property.Names = new List<string>(capacity);
                            }
                            property.Names.Add(model.Namespaces[0].Name + "." + str);
                            num4++;
                            if (num4 == capacity)
                            {
                                break;
                            }
                        }
                    }
                }
                adapter.ReleaseObject(ref target);
            }
            for (num2 = 0; num2 < num; num2++)
            {
                adapter.ReleaseObject(ref objArray[num2]);
            }
            objArray = null;
        }


        private List<MetaClass> LoadEnumerations(Adapter1C adapter, MetaModel model, object metadata)
        {
            List<MetaClass> list = new List<MetaClass>();
            object target = metadata.Get("Перечисления");
            int num = (int)target.Call("Количество", null);
            for (int i = 0; i < num; i++)
            {
                object objEnum = target.Call("Получить", new object[] { i });
                MetaClass classEnum = new MetaClass(model, (string)objEnum.Get("Имя"));
                classEnum.Synonym = (string) objEnum.Get("Synonym");
                classEnum.Kind = PersistentType.ReferenceObject;
                classEnum.BaseClass = this.EnumBase;
                classEnum.Namespace = model.Namespaces[0].Children[0];
                classEnum.Discriminator = 1;

                List<MetaEnumValue> values = new List<MetaEnumValue>();
                object objValues = objEnum.Get("ЗначенияПеречисления");
                int valuesCount = (int)objValues.Call("Количество", null);
                for (int j = 0; j < valuesCount; j++ )
                {
                    object objValue = objValues.Call("Получить", j );
                    string valueName = (string) objValue.Get("Имя");
                    string valueSynonym = (string) objValue.Get("Synonym");
                    values.Add(new MetaEnumValue(valueSynonym, valueName));
                    adapter.ReleaseObject(ref objValue);
                }
                classEnum.EnumValues = values;
                adapter.ReleaseObject(ref objValues);

                adapter.ReleaseObject(ref objEnum);
                list.Add(classEnum);
                model.Registry.Add(classEnum.FullName, classEnum);
            }
            adapter.ReleaseObject(ref target);
            return list;
        }

        private void InitNamespaces(MetaModel model)
        {
            Namespace item = new Namespace(model.Name, null);
            model.Namespaces.Add(item);
            new Namespace("Перечисления", item);
            new Namespace("Справочники", item);
            new Namespace("Документы", item);
            new Namespace("Характеристики", item);
            new Namespace("РегистрыСведений", item);
            new Namespace("РегистрыНакопления", item);
        }

        private void InitBaseClasses(MetaModel model)
        {


            // ПЕРЕЧИСЛЕНИЕ
            MetaClass enumBase = new MetaClass(model, "EnumBase");
            enumBase.Kind = PersistentType.ReferenceObject;
            enumBase.Namespace = model.Namespaces[0].Children[0];
            this.EnumBase = enumBase;
            model.Registry.Add(this.EnumBase.FullName, this.EnumBase);

            MetaProperty property2 = new MetaProperty(this.EnumBase, "Identity");
            property2.Purpose = DataPurpose.Identity;
            property2.Storage = "_Identity";
            List<System.Type> list = new List<System.Type>();
            list.Add(typeof(Guid));
            property2.Types = list;
            List<string> list2 = new List<string>();
            list2.Add("System.Guid");
            property2.Names = list2;
            property2.Size = 0x10;
            property2.AllowsNULL = false;
            MetaProperty property = property2;
            MetaField field = new MetaField(property, "_IDRRef");
            field.DbType = DbType.Binary;
            field.DbName = "binary(16)";
            field.CastToType = DbType.Guid;
            field.CastToName = "uniqueidentifier";
            property.fIdentity = field;
            this.EnumBase.Properties.Add(property);

            MetaProperty property3 = new MetaProperty(this.EnumBase, "EnumOrder");
            property3.Purpose = DataPurpose.Primitive;
            property3.Storage = "_EnumOrder";
            property3.Size = 0x13;
            property3.AllowsNULL = false;
            property3.Precision = 10;
            property3.Scale = 0;
            property3.CanBeNegative = false;
            List<System.Type> list3 = new List<System.Type>();
            list3.Add(typeof(int));
            property3.Types = list3;
            List<string> list4 = new List<string>();
            list4.Add("System.Int32");
            property3.Names = list4;
            property = property3;
            MetaField field2 = new MetaField(property, "_EnumOrder");
            field2.DbType = DbType.Decimal;
            field2.DbName = "numeric(10,0)";
            field2.CastToType = DbType.Int32;
            field2.CastToName = "int";
            property.fNumeric = field2;
            this.EnumBase.Properties.Add(property);

            // СПРАВОЧНИК
            MetaClass metaCatalog = new MetaClass(model, "CatalogBase");
            metaCatalog.Kind = PersistentType.ReferenceObject;
            metaCatalog.Namespace = model.Namespaces[0].Children[1];
            this.CatalogBase = metaCatalog;
            model.Registry.Add(this.CatalogBase.FullName, this.CatalogBase);
            
/*
            MetaProperty property4 = new MetaProperty(this.CatalogBase, "Identity");
            property4.Purpose = DataPurpose.Identity;
            property4.Storage = "_Identity";
            List<System.Type> list5 = new List<System.Type>();
            list5.Add(typeof(Guid));
            property4.Types = list5;
            List<string> list6 = new List<string>();
            list6.Add("System.Guid");
            property4.Names = list6;
            property4.Size = 0x10;
            property4.AllowsNULL = false;
            property = property4;
            MetaField field3 = new MetaField(property, "_IDRRef");
            field3.DbType = DbType.Binary;
            field3.DbName = "binary(16)";
            field3.CastToType = DbType.Guid;
            field3.CastToName = "uniqueidentifier";
            property.fIdentity = field3;
            this.CatalogBase.Properties.Add(property);

            MetaProperty property5 = new MetaProperty(this.CatalogBase, "Version");
            property5.Purpose = DataPurpose.Version;
            property5.Storage = "_Version";
            List<System.Type> list7 = new List<System.Type>();
            list7.Add(typeof(byte[]));
            property5.Types = list7;
            List<string> list8 = new List<string>();
            list8.Add("System.Byte[]");
            property5.Names = list8;
            property5.Size = 8;
            property5.AllowsNULL = false;
            property = property5;
            MetaField field4 = new MetaField(property, "_Version");
            field4.DbType = DbType.Binary;
            field4.DbName = "timestamp";
            field4.CastToType = DbType.Binary;
            field4.CastToName = "timestamp";
            property.fVersion = field4;
            this.CatalogBase.Properties.Add(property);
*/
            
            MetaProperty property6 = new MetaProperty(this.CatalogBase, "DeletionMark");
            property6.Purpose = DataPurpose.Primitive;
            property6.Storage = "_DeletionMark";
            List<System.Type> list9 = new List<System.Type>();
            list9.Add(typeof(bool));
            property6.Types = list9;
            List<string> list10 = new List<string>();
            list10.Add("System.Boolean");
            property6.Names = list10;
            property6.Size = 1;
            property6.AllowsNULL = false;
            property = property6;
            MetaField field5 = new MetaField(property, "_Marked");
            field5.DbType = DbType.Binary;
            field5.DbName = "binary(1)";
            field5.CastToType = DbType.Boolean;
            field5.CastToName = "bit";
            property.fBoolean = field5;
            this.CatalogBase.Properties.Add(property);
            
            MetaProperty property7 = new MetaProperty(this.CatalogBase, "Predefined");
            property7.Purpose = DataPurpose.Primitive;
            property7.Storage = "_IsConstant";
            List<System.Type> list11 = new List<System.Type>();
            list11.Add(typeof(bool));
            property7.Types = list11;
            List<string> list12 = new List<string>();
            list12.Add("System.Boolean");
            property7.Names = list12;
            property7.Size = 1;
            property7.AllowsNULL = false;
            property = property7;
            MetaField field6 = new MetaField(property, "_IsMetadata");
            field6.DbType = DbType.Binary;
            field6.DbName = "binary(1)";
            field6.CastToType = DbType.Boolean;
            field6.CastToName = "bit";
            property.fBoolean = field6;
            this.CatalogBase.Properties.Add(property);
            
            MetaClass class4 = new MetaClass(model, "CharacteristicBase");
            class4.Kind = PersistentType.ReferenceObject;
            class4.BaseClass = this.CatalogBase;
            class4.Namespace = model.Namespaces[0].Children[3];
            this.CharacteristicBase = class4;
            model.Registry.Add(this.CharacteristicBase.FullName, this.CharacteristicBase);
            
            MetaProperty property8 = new MetaProperty(this.CharacteristicBase, "ТипыЗначений");
            property8.Purpose = DataPurpose.Primitive;
            property8.Storage = "_ValueTypes";
            property8.AllowsNULL = true;
            property8.Size = 0;
            List<System.Type> list13 = new List<System.Type>();
            list13.Add(typeof(byte[]));
            property8.Types = list13;
            List<string> list14 = new List<string>();
            list14.Add("System.Byte[]");
            property8.Names = list14;
            property = property8;
            MetaField field7 = new MetaField(property, "_Type");
            field7.DbType = DbType.Binary;
            field7.DbName = "image";
            field7.CastToType = DbType.Binary;
            field7.CastToName = "image";
            property.fBinary = field7;
            this.CharacteristicBase.Properties.Add(property);
            
            MetaClass class5 = new MetaClass(model, "DocumentBase");
            class5.Kind = PersistentType.ReferenceObject;
            class5.Namespace = model.Namespaces[0].Children[2];
            this.DocumentBase = class5;
            model.Registry.Add(this.DocumentBase.FullName, this.DocumentBase);

            /*
            MetaProperty property9 = new MetaProperty(this.DocumentBase, "Identity");
            property9.Purpose = DataPurpose.Identity;
            property9.Storage = "_Identity";
            List<System.Type> list15 = new List<System.Type>();
            list15.Add(typeof(Guid));
            property9.Types = list15;
            List<string> list16 = new List<string>();
            list16.Add("System.Guid");
            property9.Names = list16;
            property9.Size = 0x10;
            property9.AllowsNULL = false;
            property = property9;
            MetaField field8 = new MetaField(property, "_IDRRef");
            field8.DbType = DbType.Binary;
            field8.DbName = "binary(16)";
            field8.CastToType = DbType.Guid;
            field8.CastToName = "uniqueidentifier";
            property.fIdentity = field8;
            this.DocumentBase.Properties.Add(property);
            
            MetaProperty property10 = new MetaProperty(this.DocumentBase, "Version");
            property10.Purpose = DataPurpose.Version;
            property10.Storage = "_Version";
            List<System.Type> list17 = new List<System.Type>();
            list17.Add(typeof(byte[]));
            property10.Types = list17;
            List<string> list18 = new List<string>();
            list18.Add("System.Byte[]");
            property10.Names = list18;
            property10.Size = 8;
            property10.AllowsNULL = false;
            property = property10;
            MetaField field9 = new MetaField(property, "_Version");
            field9.DbType = DbType.Binary;
            field9.DbName = "timestamp";
            field9.CastToType = DbType.Binary;
            field9.CastToName = "timestamp";
            property.fVersion = field9;
            this.DocumentBase.Properties.Add(property);
            */
            
            MetaProperty property11 = new MetaProperty(this.DocumentBase, "DeletionMark");
            property11.Purpose = DataPurpose.Primitive;
            property11.Storage = "_DeletionMark";
            List<System.Type> list19 = new List<System.Type>();
            list19.Add(typeof(bool));
            property11.Types = list19;
            List<string> list20 = new List<string>();
            list20.Add("System.Boolean");
            property11.Names = list20;
            property11.Size = 1;
            property11.AllowsNULL = false;
            property = property11;
            MetaField field10 = new MetaField(property, "_Marked");
            field10.DbType = DbType.Binary;
            field10.DbName = "binary(1)";
            field10.CastToType = DbType.Boolean;
            field10.CastToName = "bit";
            property.fBoolean = field10;
            this.DocumentBase.Properties.Add(property);
            
            MetaProperty property12 = new MetaProperty(this.DocumentBase, "Posted");
            property12.Purpose = DataPurpose.Primitive;
            property12.Storage = "_Posted";
            List<System.Type> list21 = new List<System.Type>();
            list21.Add(typeof(bool));
            property12.Types = list21;
            List<string> list22 = new List<string>();
            list22.Add("System.Boolean");
            property12.Names = list22;
            property12.Size = 1;
            property12.AllowsNULL = false;
            property = property12;
            MetaField field11 = new MetaField(property, "_Posted");
            field11.DbType = DbType.Binary;
            field11.DbName = "binary(1)";
            field11.CastToType = DbType.Boolean;
            field11.CastToName = "bit";
            property.fBoolean = field11;
            this.DocumentBase.Properties.Add(property);
            
            MetaProperty property13 = new MetaProperty(this.DocumentBase, "Date");
            property13.Purpose = DataPurpose.Primitive;
            property13.Storage = "_Date";
            List<System.Type> list23 = new List<System.Type>();
            list23.Add(typeof(DateTime));
            property13.Types = list23;
            List<string> list24 = new List<string>();
            list24.Add("System.DateTime");
            property13.Names = list24;
            property13.Size = 8;
            property13.DatePart = DatePart.DateTime;
            property13.Periodicity = Periodicity.Second;
            property13.AllowsNULL = false;
            property = property13;
            MetaField field12 = new MetaField(property, "_Date_Time");
            field12.DbType = DbType.DateTime;
            field12.DbName = "datetime";
            field12.CastToType = DbType.DateTime;
            field12.CastToName = "datetime";
            property.fDateTime = field12;
            this.DocumentBase.Properties.Add(property);
            
            MetaClass class6 = new MetaClass(model, "AccRegBase");
            class6.Kind = PersistentType.ValueObject;
            class6.Namespace = model.Namespaces[0].Children[5];
            this.AccRegBase = class6;
            model.Registry.Add(this.AccRegBase.FullName, this.AccRegBase);
            
            MetaProperty property14 = new MetaProperty(this.AccRegBase, "Period");
            property14.Purpose = DataPurpose.Primitive;
            property14.Storage = "_Period";
            List<System.Type> list25 = new List<System.Type>();
            list25.Add(typeof(DateTime));
            property14.Types = list25;
            List<string> list26 = new List<string>();
            list26.Add("System.DateTime");
            property14.Names = list26;
            property14.Size = 8;
            property14.DatePart = DatePart.DateTime;
            property14.Periodicity = Periodicity.Second;
            property14.AllowsNULL = false;
            property = property14;
            MetaField field13 = new MetaField(property, "_Period");
            field13.DbType = DbType.DateTime;
            field13.DbName = "datetime";
            field13.CastToType = DbType.DateTime;
            field13.CastToName = "datetime";
            property.fDateTime = field13;
            this.AccRegBase.Properties.Add(property);
            
            MetaProperty property15 = new MetaProperty(this.AccRegBase, "IsActive");
            property15.Purpose = DataPurpose.Primitive;
            property15.Storage = "_IsActive";
            List<System.Type> list27 = new List<System.Type>();
            list27.Add(typeof(bool));
            property15.Types = list27;
            List<string> list28 = new List<string>();
            list28.Add("System.Boolean");
            property15.Names = list28;
            property15.Size = 1;
            property15.AllowsNULL = false;
            property = property15;
            MetaField field14 = new MetaField(property, "_Active");
            field14.DbType = DbType.Binary;
            field14.DbName = "binary(1)";
            field14.CastToType = DbType.Boolean;
            field14.CastToName = "bit";
            property.fBoolean = field14;
            this.AccRegBase.Properties.Add(property);
            
            MetaProperty property16 = new MetaProperty(this.AccRegBase, "LineNumber");
            property16.Purpose = DataPurpose.Primitive;
            property16.Storage = "_LineNumber";
            property16.Size = 0x13;
            property16.Precision = 9;
            property16.Scale = 0;
            property16.CanBeNegative = false;
            property16.AllowsNULL = false;
            List<System.Type> list29 = new List<System.Type>();
            list29.Add(typeof(int));
            property16.Types = list29;
            List<string> list30 = new List<string>();
            list30.Add("System.Int32");
            property16.Names = list30;
            property = property16;
            MetaField field15 = new MetaField(property, "_LineNo");
            field15.DbType = DbType.Decimal;
            field15.DbName = "numeric(9,0)";
            field15.CastToType = DbType.Int32;
            field15.CastToName = "int";
            property.fNumeric = field15;
            this.AccRegBase.Properties.Add(property);

/*
            MetaClass infoRegBase = new MetaClass(model, "InfoRegBase");
            infoRegBase.Namespace = model.Namespaces[0].Children[6];
*/

            
            MetaClass class7 = new MetaClass(model, "TablePart");
            class7.Kind = PersistentType.DependentObject;
            class7.Namespace = model.Namespaces[0];
            this.TablePart = class7;
            model.Registry.Add(this.TablePart.FullName, this.TablePart);
            
/*
            MetaProperty property17 = new MetaProperty(this.TablePart, "KeyField");
            property17.Purpose = DataPurpose.Primitive | DataPurpose.CompoundKey;
            property17.Storage = "_KeyField";
            property17.Size = 4;
            property17.Precision = 10;
            property17.Scale = 0;
            property17.CanBeNegative = false;
            List<System.Type> list31 = new List<System.Type>();
            list31.Add(typeof(int));
            property17.Types = list31;
            List<string> list32 = new List<strin g>();
            list32.Add("System.Int32");
            property17.Names = list32;
            property = property17;
            MetaField field16 = new MetaField(property, "_KeyField");
            field16.DbType = DbType.Binary;
            field16.DbName = "binary(4)";
            field16.CastToType = DbType.Int32;
            field16.CastToName = "int";
            property.fNumeric = field16;
            this.TablePart.Properties.Add(property);
*/
        }



    }
}
